const router = require('express').Router()
const {db,md5hash,jwt,time,jwtRouter,upload,MulterError} = require('../util')
const {uploads} = require('../config')

jwtRouter.get('/integral',(req,res)=>{
    let sql = 'SELECT COUNT(*) c FROM ti_user_questions WHERE user_id = ? AND isright=1'
    db.query(sql, req.user.id, (err, results)=>{
        res.json({
            ok: 1,
            data: {
                integral: results[0].c
            }
        })
    })
})

router.post('/access_token', (req,res)=>{
    let sql = 'SELECT id,face,password FROM ti_users WHERE username=?'
    db.query(sql, req.body.username, (err,results)=>{
        if(results[0]) {
            if(results[0].password == md5hash(req.body.password)) {
                let face = results[0].face
                let token = jwt({
                    id: results[0].id
                })
                res.json({
                    ok: 1,
                    data: {
                        token,
                        face
                    }
                })
            } else {
                res.json({
                    ok: 0,
                    error: '密码错误'
                })
            }
        } else {
            res.json({
                ok: 0,
                error: '用户名错误'
            })
        }  
    })
})

router.post('/register', (req,res)=>{
    let sql = 'SELECT id FROM ti_users WHERE username=?'
    db.query(sql, req.body.username, (err,results)=>{
        if(results[0]) {
            res.json({
                ok: 0,
                error: '用户名已经存在'
            })
        } else {
            sql = 'INSERT INTO ti_users SET ?'
            let data = {
                username: req.body.username,
                password: md5hash(req.body.password),
                regtime: time()
            }
            db.query(sql, data, (err,results,fields)=>{
                res.json({
                    ok: 1
                })
            })
        }
    })
})

jwtRouter.post('/passwords', (req,res)=>{
    let sql = 'SELECT password FROM ti_users WHERE id=?'
    db.query(sql, req.user.id, (err,results,fields)=>{
        if(results[0].password == md5hash(req.body.old_password)) {
            sql = 'UPDATE ti_users SET ? WHERE id=?'
            let data = {
                password: md5hash(req.body.password)
            }
            db.query(sql, [data, req.user.id], (err,results,fields)=>{
                res.json({
                    ok: 1
                })
            })
        } else {
            res.json({
                ok: 0,
                error: '原密码不正确'
            })
        }
    })
})

jwtRouter.post('/face', (req,res)=>{
    let sql = 'UPDATE ti_users SET ? WHERE id=?'
    let data = {
        face: req.body.face
    }
    db.query(sql, [data, req.user.id], (err,results,fields)=>{
        res.json({
            ok: 1
        })
    }) 
})

jwtRouter.get('/member_cate_question_rate', (req,res)=>{
    let sql = `SELECT a.*,
                    (SELECT COUNT(*) FROM ti_user_questions c
                    LEFT JOIN ti_questions d ON c.question_id=d.id	
                    WHERE c.user_id=1 AND c.isright=1 AND d.category_id=a.id)*100/
                    (SELECT COUNT(*) FROM ti_questions b WHERE a.id=b.category_id) right_rate
                FROM ti_categories a`
    db.query(sql, req.user.id, (err,results)=>{
        res.json({
            ok: 1,
            data: results
        })
    })
})

jwtRouter.post('/upload', (req,res)=>{
    upload.single('file')(req, res, function (err) {
        if (err) {
          // 发生错误
          res.json({
              ok: 0,
              error: err.message
          })
        } else {
            res.json({
                ok: 1,
                data: {
                    path: uploads.domain + '/' + req.file.path
                }
            })
        }   
    })
})

module.exports = [router, jwtRouter]